package com.mingqi.enetconnected.notice;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;

import com.mingqi.enetconnected.base.MyApplication;

public class NotifyTools {
    public final static int NORMAL_NOTIFYID = 8815;
    private static NotificationManager mNM;

    public static void showNotificationNormal(Service serv, int requestCode, int iconId, String title, String text,
                                                Intent intent  ) {

        try {

            serv.stopForeground(true);

            Notification.Builder builder = null;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                builder = createChannels(serv);
            } else {
                builder = new Notification.Builder(serv);
            }
            if (intent != null) {
                PendingIntent contentIntent = PendingIntent.getActivity(serv, requestCode, intent, 0);
                builder.setContentIntent(contentIntent);
            }

            builder.setContentTitle(title);
            builder.setContentText(text);
            builder.setTicker(title);
            builder.setLargeIcon(BitmapFactory.decodeResource(serv.getResources(), iconId));
            builder.setSmallIcon(iconId);// 设置图标
            builder.setWhen(System.currentTimeMillis());// 设置通知来到的时间
         //   builder.setAutoCancel(false);

            Notification notification;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {// 4.1及以上
                notification = builder.build();
            } else {
                notification = builder.getNotification();
            }
           notification.flags = Notification.FLAG_ONGOING_EVENT ;
            if (((MyApplication) serv.getApplication()).locationService != null) {
                ((MyApplication) serv.getApplication()).locationService.enableLocInForeground(NORMAL_NOTIFYID, notification);
            }
            serv.startForeground(NORMAL_NOTIFYID, notification);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @TargetApi(Build.VERSION_CODES.O)
    public static Notification.Builder createChannels(Service serv) {
        if (mNM == null) {
            mNM = (NotificationManager) serv.getSystemService(Context.NOTIFICATION_SERVICE);
        }
        // create android channel
        NotificationChannel androidChannel = new NotificationChannel(NotifyUtil.ANDROID_CHANNEL_ID,
                NotifyUtil.ANDROID_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
        // Sets whether notifications posted to this channel should display notification lights
        androidChannel.enableLights(true);
        // Sets whether notification posted to this channel should vibrate.
        androidChannel.enableVibration(true);
        // Sets the notification light color for notifications posted to this channel
        androidChannel.setLightColor(Color.GREEN);
        // Sets whether notifications posted to this channel appear on the lockscreen or not
        androidChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);

        mNM.createNotificationChannel(androidChannel);
        return new Notification.Builder(serv, NotifyUtil.ANDROID_CHANNEL_ID).setAutoCancel(false);

    }
}
